package guoan.service;

import guoan.core.common.WebConfig;
import guoan.core.dto.DataGridModel;
import guoan.core.dto.PageResponse;
import guoan.core.dto.RestResponse;
import guoan.domain.Train;
import guoan.repository.TrainRepository;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Preconditions;

@Service
public class TrainService extends PageSerivce<Train> {
	protected final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private TrainRepository trainRepository;

	public PageResponse<Train> page(DataGridModel dataGridModel) {
		// TODO Auto-generated method stub
		return pageResponse(trainRepository, dataGridModel);
	}

	public RestResponse<Boolean> update(Train train) {
		// TODO Auto-generated method stub
		Preconditions.checkNotNull(train, "参数错误");
		Preconditions.checkNotNull(train.getIdCard(), "参数错误");
		Preconditions.checkNotNull(train.getTrainTel(), "参数错误");
		Preconditions.checkNotNull(train.getTrainName(), "参数错误");
		Preconditions.checkNotNull(train.getId(), "参数错误");
		Preconditions.checkNotNull(train.getPassword(), "参数错误");
		Preconditions.checkNotNull(train.getSchoolName(), "参数错误");
		Preconditions.checkNotNull(train.getTrainNo(), "参数错误");
		Preconditions.checkNotNull(train.getCarAddr(), "参数错误");

		Train oTrain = trainRepository.findOne(train.getId());

		Preconditions.checkNotNull(oTrain, "参数错误");

		oTrain.setPassword(train.getPassword());
		oTrain.setTrainTel(train.getTrainTel());
		oTrain.setIdCard(train.getIdCard());
		oTrain.setTrainName(train.getTrainName());
		oTrain.setSchoolName(train.getSchoolName());
		oTrain.setTrainNo(train.getTrainNo());
		oTrain.setCarAddr(train.getCarAddr());

		oTrain.setUpdateTime(new DateTime());
		oTrain = trainRepository.save(oTrain);
		return RestResponse.ok(true);
	}

	public RestResponse<Boolean> add(Train train) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		Preconditions.checkNotNull(train, "参数错误");
		Preconditions.checkNotNull(train.getIdCard(), "参数错误");
		Preconditions.checkNotNull(train.getTrainTel(), "参数错误");
		Preconditions.checkNotNull(train.getTrainName(), "参数错误");
		Preconditions.checkNotNull(train.getPassword(), "参数错误");
		Preconditions.checkNotNull(train.getSchoolName(), "参数错误");

		train.setCreateTime(new DateTime());
		train.setUpdateTime(train.getCreateTime());
		train = trainRepository.save(train);
		if (train != null && train.getId() != null) {
			return RestResponse.ok(true);
		}
		return RestResponse.error("系统错误，请联系系统管理人员");
	}

	public RestResponse<Boolean> del(Long id) {
		Preconditions.checkNotNull(id, "参数错误");
		Train oTrain = trainRepository.findOne(id);
		trainRepository.delete(oTrain);
		return RestResponse.ok(true);
	}

	public Train findOne(Long id) {
		// TODO Auto-generated method stub
		return trainRepository.findOne(id);
	}

	public Train findByOpenid(String openid) {
		// TODO Auto-generated method stub
		return trainRepository.findByOpenid(openid);

	}

	public RestResponse<Boolean> join(Train train, HttpServletRequest request) {
		// TODO Auto-generated method stub
		Preconditions.checkNotNull(train, "参数错误");

		train = trainRepository.findByTrainNameAndTrainTelAndPasswordAndTrainNo(
				Preconditions.checkNotNull(train.getTrainName(), "参数错误"),
				Preconditions.checkNotNull(train.getTrainTel(), "参数错误"),
				Preconditions.checkNotNull(train.getPassword(), "参数错误"),
				Preconditions.checkNotNull(train.getTrainNo(), "参数错误"));

		if (train == null) {
			return RestResponse.error("绑定信息错误，请重新填写");
		} else {
			// 绑定的时候获取openid 保存在train中
			HttpSession session = request.getSession();
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);

			train.setOpenid(openid);
			trainRepository.save(train);
			return RestResponse.ok(true);
		}
	}

	public void selfUpdate(Train train) {
		trainRepository.save(train);
	}

}